package com.usian.wemedia.controller;

import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {
    @Autowired
    private WmNewsService wmNewsService;
    //文章列表查询
    @RequestMapping("list")
    @Override
    public ResponseResult findList(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findList(dto);
    }

    //文章保存
    @RequestMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if(wmNews.getStatus().equals(WmNews.Status.SUBMIT.getCode())){
            return wmNewsService.saveNews(wmNews,(short) 1);
        }else {
            return wmNewsService.saveNews(wmNews,(short) 0);
        }
    }

    //根据文章id查询回显
    @GetMapping("/one/{newId}")
    @Override
    public ResponseResult findWmNewsById(@PathVariable Integer newId) {
        return wmNewsService.findWmNewsById(newId);
    }

    //admin端审核 根据id查询文章详情
    @Override
    @GetMapping("/findOne/{id}")
    public WmNews findById(@PathVariable Integer id) {
        return wmNewsService.getById(id);
    }

    //admin端审核 保存修改文章状态
    @Override
    @PostMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        boolean result = wmNewsService.updateById(wmNews);
        if (result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
